﻿/*
 * Twipler - Twitter Web Client with features.
 *  
 *  Copyright (C) 2009, 2010. Ian Quigley
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details. 
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 */


using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

using Twipler.App.Parse.Html;
using Twipler.App.Render.Tree;
using Twipler.Data.Entity.Twipler;
using Twipler.WebApp;

namespace Settings.Sandbox
{
    [RequireUser(VisitorRole.Registered)]
    public partial class HtmlParse : System.Web.UI.Page
    {
        private string[] Description = new string[]
                                           {
                                               "HTML is parsed for template markers which are inline replaced. This first example just displays the HTML you submit.",
                                               "In this demo, the framework replaces <span class=\"code\">#!field source=\"Wild.Random()\"!#</span> with a random number.<br/><br/><span class=\"code\">Wild.Random</span> is a static method of <span class=\"code\">App.Entity.Wild</span> class.",
                                               "Fixed iteration can be achived using loop. <br/>In this example, we call the static method Count on the App.Entity.Wild class to produce a list of numbers 0 to 5.",
                                               "Another fixed iteration loop, but this time calling <span class=\"code\">Wild.Random</span> inside the loop.",
                                               "And another fixed iteration loop, but this time <span class=\"code\">index=\"i\"</span> puts the value into a variable <b>i</b> and this is displayed using <span class=\"code\">#!variable i!#</span>",
                                               "Code! (feel free to skip)<br/><br/>Remember that <span class=\"code\">#!field ...!#</span> is a field evaluator. We can use it to evaluate some stack operator microcode. <span class=\"code\">Push 3 onto the stack</span> and field will display the last value on the stack. <br/><br/>Try it.",
                                               "Code! (feel free to skip)<br/><br/>Ok, so what is 3 + 2? Or rather what is 3 2 +",
                                               "Code! (feel free to skip)<br/><br/>Now for a boolean result, we'll see if <span class=\"code\">3 2 + 2 * 10 =</span><br/>For those who don't read <a href=\"http://en.wikipedia.org/wiki/Reverse_polish\">Reverse Polish</a>, is (3 + 2) * 2 = 10 ?",
                                               "Code! (feel free to skip)<br/><br/>A boolean result can be used for an HTML template IF statement. The <span class=\"code\">#!if ...!# #!endif!#</span> inner Html is displayed if the condition is true.",
                                               "Loops and code - the last example.<br/><br/>The mnemonic <span class=\"code\">pushv</span> will push a variable onto the stack (see loop example). We can use this in the IF html template instruction."
                                           };

        private string[] Tests = new string[]
                                     {
                                         "<h3>Html demo</h3><p>Hello world.</p>",
                                         "<p>This is a random number #!field source=\"Wild.Random()\"!#</p>",
                                         "<p>Do it five "+ Environment.NewLine +"#!loop source=\"Wild.Count(0,5)\"!#times #!loopend!#</p>",
                                         "<p>Call this phone number:" + Environment.NewLine + "#!loop source=\"Wild.Count(0,9)\"!##!field source=\"Wild.Random()\"!##!loopend!#</p>",
                                         "<p>Counting " + Environment.NewLine + "#!loop source=\"Wild.Count(1,10)\" index=\"i\"!#\n#!variable i!#\n#!loopend!#\n</p>",
                                         "Remember #!field program=\"pushi 3\"!# is the magic number!",
                                         "And 3 2 + is: #!field program=\"pushi 3 pushi 2 addi \"!# ",
                                         "(3 + 2) * 2 = 10? Computer says: #!field program=\"pushi 3 pushi 2 addi pushi 2 muli pushi 10 EqI \"!# ",
                                         "#!if expr=\"pushi 3 pushi 2 addi pushi 5 EqI \"!#this is the true section#!else!#this is the false section#!endif!# ",
                                         "#!loop source=\"Wild.Count(1,10)\" index=\"X\"!#\n\nX = #!variable X!# which is\n#!if expr=\"PushV X PushI 5 GtI \"!#<b>greater than</b>#!else!#<i>not greater than</i>#!endif!#\n 5.<br/>\n\n#!loopend!# "
            
            
                                     };


        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            int test = 0;
            if (Request.QueryString["test"] != null)
                if (int.TryParse(Request.QueryString["test"], out test) == false)
                    test = 0;

            if (test >= Tests.Count())
                test = 0;

            txtBox.Text = Tests[test];

            if (test < Description.Count())
                litDescription.Text = Description[test];
            else
                litDescription.Text = "No description";

        }

        protected void btnSub_Click(object sender, EventArgs e)
        {
            try
            {
                Template text = new Template(txtBox.Text);

                AbstractItem root = text.Parse();

                StringBuilder html = new StringBuilder();

                Twipler.App.Machine.Environment env = new Twipler.App.Machine.Environment();

                root.Render(html, env);

                litRender.Text = html.ToString();

                
            }
            catch (Exception ex)
            {
                litRender.Text = "<span class=\"error\">Exception!</span><br/>" + ex.Message;
            }

        }

    }
}